< previous page page_465 next page >

Page 465
The Second Method for Finding Values in Combo Boxes
Another technique for finding a value in a combo box is to set the Text property to the value youre seeking. This forces the combo box to be positioned to that value. The problem with this technique is that if the value is not found, a trappable runtime error is generated. Because the error is trappable, you need to check to see whether an error is generated. If it is, you set the ListIndex property to -1 to force the combo box to have no value selected:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub StateFind2(ByVal strState As String)
   On Error Resume Next
   cboStates.Text = strState
   If Err.Number Then
      cboStates.ListIndex = -1
   End If
End Sub
The ComboBox_Click Event
When you click a combo box to drop down the list portion, the Click event is fired. This is great when the user selects a new value in the list, but if the user doesnt change the value, the Click event is still fired. If youve written code within the Click event to enable a Save button, it will be fired whether or not a change is made. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cboStates_Click ()
   cmdSave.Enabled = True
End Sub
Instead, you need to detect whether the value has been changed in the combo box. To accomplish this, you need to write code in the GotFocus() event as well as the Click event. In the GotFocus() event, you grab the value in the Text property and copy it into the Tag property, like this:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cboStates_GotFocus()
   cboStates.Tag = cboStates.Text
End Sub
Then you change the Click event so that you compare the Tag property against the Text property. If they are different, you enable the Save button and copy the Text property back into the Tag property:

 
< previous page page_465 next page >

If you like this book, buy it!